/*业务逻辑(左侧加号展开阅读):
 *	1.获取前台商家填写的注册信息
 *	2.检验数据合法性
 *		1) 检验是否有输入空值			错误代号：3
 *		2) 检验两次密码是否一致		错误代号：2
 *		3) 检验该手机号是否已注册		错误代号：1
 *		4) 系统抛出的其他未知异常		错误代号：0
 *	  将错误代号传至fail_to_register.jsp，输出错误信息。
 *	3.连接数据库，添加商家表的用户信息
 * 
 * 
 * 
 */


package OSMS.action.Boss;

import OSMS.dao.BossDao;

import OSMS.entity.Boss;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@SuppressWarnings("serial")
public class Boss_Register_Servlet extends HttpServlet {

	BossDao bdao = new BossDao();
    boolean flag = false;

    @SuppressWarnings("unchecked")
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");

        try {
            // 1. 获取页面数据
            String bname = request.getParameter("bname").trim();
            String btel = request.getParameter("btel").trim();
            String baddress = request.getParameter("baddress").trim();
            String bpwd = request.getParameter("bpwd").trim();
            String bpwd_assure = request.getParameter("bpwd_again").trim();

            //2. 判断是否有空值
            if(bname.length() != 0 && btel.length() != 0  &&
                    baddress.length() != 0  && bpwd.length() != 0  && bpwd_assure.length() != 0 ) {

                //3. 判断两次密码是否相等
                if (bpwd.equals(bpwd_assure)) {

                    //4. 判断用户是否已存在
                    List<Boss> BE = bdao.getBossByTel(btel);
                    if (BE.size() == 0) {

                        //4. 加入用户
                        try{
                        	Boss boss = new Boss();
                        	boss.setBname(bname);
                        	boss.setBtel(btel);
                        	boss.setBaddress(baddress);
                        	boss.setBpwd(bpwd);
                            flag = bdao.insertBoss(boss);

                            request.getRequestDispatcher("successful_to_register.jsp").forward(request, response);
                            return;
                            
                        }catch (Exception ex_1){
//                            JOptionPane.showMessageDialog(null,
//                                    "未知错误" + ex_1, "未知错误" + ex_1, JOptionPane.ERROR_MESSAGE);
                            ex_1.printStackTrace();
                            request.setAttribute("fail_code", 0);
                            request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                            return;
                        }
                    } else {
//                        JOptionPane.showMessageDialog(null,
//                                "用户已存在", "用户已存在", JOptionPane.ERROR_MESSAGE);
                        request.setAttribute("fail_code", 1);
                        request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                        return;
                    }
                } else {
//                    JOptionPane.showMessageDialog(null,
//                            "密码不相等", "密码不相等", JOptionPane.ERROR_MESSAGE);
                    request.setAttribute("fail_code", 2);
                    request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                    return;
                }
            }else {
//                JOptionPane.showMessageDialog(null,
//                        "数据有误", "数据有误", JOptionPane.ERROR_MESSAGE);
                
                request.setAttribute("fail_code", 3);
                request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
                return;
            }

        } catch(Exception ex_2) {
//            JOptionPane.showMessageDialog(null,
//                    "未知错误" + ex_2, "未知错误" + ex_2, JOptionPane.ERROR_MESSAGE);
            request.setAttribute("fail_code", 0);
            request.getRequestDispatcher("fail_to_register.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
